package com.lhy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.ShopCart;
import com.lhy.domain.Tea;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/shoppingcart")
public class ShoppingCartController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("")
	public ModelAndView list(HttpSession session,String pageNow){
		
		ModelAndView mv = new ModelAndView();
		try {
			Integer pagenow = 1;
			if(!StringUtils.isEmpty(pageNow)){
				pagenow = Integer.valueOf(pageNow);
			}
			
			Integer sequence = Integer.valueOf(session.getAttribute("usersequence").toString());
			Integer count = userService.queryAllShopCartCountBysequence(sequence);
			Page page = new Page(count, pagenow);
			Integer startIndex = pagenow*page.getPageSize()-3;
			Integer endIndex = pagenow*page.getPageSize();
			List<ShopCart> list = userService.queryShopCartBySequencepage(sequence,startIndex,endIndex);
			mv.addObject("page",page);

			mv.addObject("list",list);
			mv.setViewName("shoppingcart");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/remove")
	public @ResponseBody
	Map<String, Object> add(HttpSession session,String shopcartId)  {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");
		
		try{
			Integer sequence = Integer.valueOf(session.getAttribute("usersequence").toString());
			userService.removeShopCart(sequence,Integer.valueOf(shopcartId));
		}catch(Exception e){
				result.setCode("0");
				result.setDes(e.getMessage());
			
		}
		returnMap.put("result", result);
		
		return returnMap;
	}
}
